package com.droid.library.activity;

import android.app.Activity;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.annotation.DrawableRes;
import android.support.v4.content.ContextCompat;
import android.view.View;
import android.widget.ImageView;

import com.droid.library.app.BaseActivity;
import com.droid.library.thirdplatform.imageloader.CoreImageLoader;
import com.droid.library.utils.text.StringUtils;
import com.xinye.lib.R;

import java.io.File;

import com.droid.library.widget.photoview.OnPhotoTapListener;
import com.droid.library.widget.photoview.PhotoView;

/**
 *  大图
 *
 *  @author wangheng
 */
public class BigImageActivity extends BaseActivity {

    private static final String KEY_DRAWABLE_ID = "keyDrawableId";
    private static final String KEY_IMAGE_URL = "keyImageURL";
    private static final String KEY_IMAGE_PATH = "keyImagePath";


    private static final int INVALID_ID = -1;

    private PhotoView mPhotoView;
    private String mUrl;
    private String mImagePath;
    private int mDrawableId = INVALID_ID;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        if(getIntent() == null || getIntent().getExtras() == null){
            finishActivity();
            return;
        }
        mUrl = getIntent().getExtras().getString(KEY_IMAGE_URL,null);
        mImagePath = getIntent().getExtras().getString(KEY_IMAGE_PATH,null);
        mDrawableId = getIntent().getExtras().getInt(KEY_DRAWABLE_ID,INVALID_ID);

        if(StringUtils.isNullOrEmpty(mUrl) && mDrawableId == INVALID_ID
                && StringUtils.isNullOrEmpty(mImagePath)){
            finishActivity();
            return;
        }


        setContentView(R.layout.activity_big_image);

        mPhotoView = (PhotoView) findViewById(R.id.iv_photo);
        mPhotoView.setScaleType(ImageView.ScaleType.FIT_CENTER);

        if(mDrawableId != INVALID_ID){
            Drawable bitmap = ContextCompat.getDrawable(this, mDrawableId);
            mPhotoView.setImageDrawable(bitmap);
        }else if(!StringUtils.isNullOrEmpty(mImagePath)){
            try{
                File file = new File(mImagePath);
                CoreImageLoader.getInstance().display(mPhotoView,file);
            }catch (Exception e){
                e.printStackTrace();
            }
        }else{
            CoreImageLoader.getInstance().display(mPhotoView,mUrl);
        }


        // Lets attach some listeners, not required though!
        mPhotoView.setOnPhotoTapListener(new PhotoTapListener());
    }

    @Override
    protected String getPageName() {
        return "BigImageActivity";
    }

    private class PhotoTapListener implements OnPhotoTapListener {

        @Override
        public void onPhotoTap(ImageView view, float x, float y) {
            float xPercentage = x * 100f;
            float yPercentage = y * 100f;

            finishActivity();

        }
    }

    @Override
    public void onClick(View v) {

    }

    public static void launch(Activity activity,@DrawableRes int drawableId){
        Intent intent = new Intent(activity,BigImageActivity.class);

        Bundle bundle = new Bundle();
        bundle.putInt(KEY_DRAWABLE_ID,drawableId);
        intent.putExtras(bundle);

        activity.startActivity(intent);
    }
    public static void launch(Activity activity,String imageUrl){
        Intent intent = new Intent(activity,BigImageActivity.class);

        Bundle bundle = new Bundle();
        bundle.putString(KEY_IMAGE_URL,imageUrl);
        intent.putExtras(bundle);

        activity.startActivity(intent);
    }
    public static void launch(Activity activity,File file){
        Intent intent = new Intent(activity,BigImageActivity.class);

        Bundle bundle = new Bundle();
        if(file != null && file.exists()) {
            bundle.putString(KEY_IMAGE_PATH, file.getAbsolutePath());
        }
        intent.putExtras(bundle);

        activity.startActivity(intent);
    }
}
